package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.ui.mvp.contract.HdWalletSendTransactionContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletLocalContract;

import org.json.JSONException;
import org.web3j.crypto.CipherException;

import java.io.IOException;

import javax.inject.Inject;

@ActivityScope
public class HdWalletSendTransactionPresenter extends BasePresenter<HdWalletSendTransactionContract.Model, HdWalletSendTransactionContract.View> {


    @Inject
    public HdWalletSendTransactionPresenter(HdWalletSendTransactionContract.Model model, HdWalletSendTransactionContract.View rootView) {
        super(model, rootView);
    }


    public void getGasPrice() {
        addSubscribe(
                mModel.getGasPrice()
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView, false) {

                            @Override
                            protected void onStart() {
                                super.onStart();
                                mView.showLoading("");
                            }

                            @Override
                            public void onNext(String data) {
                                //super.onNext(data);
                                mView.onGetGasPriceSuccess(data);
                                //mView.hideLoading();
                            }

                            @Override
                            public void onComplete() {
                                //super.onComplete();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                mView.hideLoading();
                            }
                        })
        );
    }


    public void sendTransaction(String hex) {
        addSubscribe(
                mModel.sendTransaction(hex)
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView) {
                            @Override
                            protected void onStart() {
                                super.onStart();
                                mView.showLoading("交易处理中");
                            }

                            @Override
                            public void onComplete() {
                                mView.hideLoading();
                            }

                            @Override
                            public void onNext(String data) {
                                mView.onSendTransactionSuccess(data);
                            }

                            @Override
                            public void onError(Throwable e) {
                                mView.showError("交易失败");
                            }
                        })
        );
    }


    public void getNonce(String addr) {
        addSubscribe(
                mModel.getNonce(addr)
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView) {
                            @Override
                            protected void onStart() {
                                super.onStart();
                                mView.showLoading("交易处理中");
                            }

                            @Override
                            public void onComplete() {
                                mView.hideLoading();
                            }

                            @Override
                            public void onNext(String data) {
                                try {
                                    mView.onGetNonce(data);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                } catch (CipherException e) {
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        })
        );
    }


    public void getEthPrice() {
        addSubscribe(
                mModel.getEthPrice()
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView, false) {

                            @Override
                            public void onNext(String data) {
                                // super.onNext(data);
                                mView.onGetEthPriceSuccessed(data);
                                mView.hideLoading();
                            }

                            @Override
                            public void onComplete() {
                                // super.onComplete();
                                mView.hideLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                mView.hideLoading();
                            }
                        })
        );
    }


    public void getUsdCnyRate(String from, String to) {
        addSubscribe(
                mModel.getUsdCnyRate(from, to)
                        .compose(RxUtils.<String>rxSchedulerHelper())
                        .subscribeWith(new CommonSubscriber<String>(mView, false) {

                            @Override
                            public void onNext(String data) {
                                //  super.onNext(data);
                                mView.onGetUsdCnyRateSuccessed(data);
                                //mView.hideLoading();
                            }

                            @Override
                            public void onComplete() {
                                //super.onComplete();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                mView.hideLoading();
                            }
                        })
        );
    }


}


